package com.cwh.taskcenter.task.executor.query;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.fastjson.JSON;
import com.cwh.taskcenter.domain.task.gateway.TaskGateWay;
import com.cwh.taskcenter.domain.task.model.Task;
import com.cwh.taskcenter.task.dto.data.TaskPageVO;
import com.cwh.taskcenter.task.dto.query.TaskListByPageQry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cwh
 */
@Component
public class TaskByPageQryExe {

    @Autowired
    private TaskGateWay taskGateWay;

    public PageResponse<TaskPageVO> execute(TaskListByPageQry qry) {
        PageResponse<Task> page = taskGateWay.page(qry);
        List<TaskPageVO> taskPageVOList = page.getData().stream()
                .map(task -> {
                    TaskPageVO vo = new TaskPageVO();
                    vo.setId(task.getId());
                    vo.setTaskTitle(task.getTaskTitle());
                    vo.setTaskType(task.getTaskType());
                    vo.setClaimType(task.getClaimType());
                    vo.setStartTime(LocalDateTimeUtil.formatNormal(task.getStartTime()));
                    vo.setEndTime(LocalDateTimeUtil.formatNormal(task.getEndTime()));
                    vo.setTaskStatus(task.getTaskStatus().name());
                    vo.setTaskRule(JSON.toJSONString(task.getTaskRules()));
                    vo.setCreateTime(LocalDateTimeUtil.formatNormal(task.getCreateTime()));
                    return vo;
                })
                .collect(Collectors.toList());
        return PageResponse.of(taskPageVOList, page.getTotalCount(), qry.getPageSize(), qry.getPageIndex());
    }


}
